package com.csist.dao;

import com.csist.bean.Teacher;
import com.csist.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @ClassName TeacherDao
 * @Description TODO
 * @Author User
 * @Date 2021/6/25 16:17
 * @ModifyDate 2021/6/25 16:17
 * @Version 1.0
 */
public class TeacherDao {

    public Teacher login(String user, String psw){
        Connection conn = JDBCUtil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from teacher where t_username = ? and t_password = ?";
        Teacher teacher = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,user);
            ps.setString(2,psw);
            rs = ps.executeQuery();
            if (rs.next()){
                teacher = new Teacher(rs.getInt(1),rs.getString(2),rs.getBoolean(3),rs.getDate(4),
                        rs.getString(5),rs.getInt(6),rs.getString(7),rs.getString(8),rs.getString(9));

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(conn,ps,rs);
        }
        return teacher;
    }
    public boolean register(Teacher teacher){
        Connection conn = JDBCUtil.getConnection();
        PreparedStatement ps = null;
        String sql = "insert into teacher value(default,?,?,?,?,?,?,?,?)";
        boolean finis = false;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,teacher.getT_name());
            ps.setBoolean(2,teacher.isGender());
            ps.setDate(3,teacher.getT_bri());
            ps.setString(4,teacher.getT_phone());
            ps.setInt(5,teacher.getT_state());
            ps.setString(6,teacher.getT_dec());
            ps.setString(7,teacher.getT_username());
            ps.setString(8,teacher.getT_password());
            int isOk = ps.executeUpdate();
            finis = isOk==1?true:false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(conn,ps);
        }
        return finis;

    }
}
